import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

typedef ChipFormChange<T> = Function(List<T> selectedItems, T item);

class ChipFormData<T> {
  String title;
  T value;

  ChipFormData({this.title, this.value});
}

class ChipFormField<T> extends StatelessWidget {
  final dynamic title;
  final List<ChipFormData<T>> items;
  final List<T> selectedItems;
  final bool multiple;
  final ChipFormChange<T> onChanged;
  final bool enabled;

  const ChipFormField({
    Key key,
    @required this.title,
    @required this.items,
    @required this.selectedItems,
    this.multiple = false,
    this.onChanged,
    this.enabled = true,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomListTitle(
      title: title,
      trailing: Expanded(
        child: Container(
          margin: EdgeInsets.only(
            left: 30.w,
          ),
          child: SingleChildScrollView(
            physics: BouncingScrollPhysics(),
            scrollDirection: Axis.horizontal,
            reverse: true,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: items
                  .map((item) => Container(
                        margin: EdgeInsets.only(
                          right: ScreenUtil.instance.setWidth(items.last == item ? 0 : 20),
                        ),
                        child: Chip(
                          title: item.title,
                          isSelected: selectedItems.contains(item.value),
                          onTap: enabled
                              ? () {
                                  if (onChanged != null) {
                                    var flag = selectedItems.contains(item.value);

                                    var copy = List<T>.from(selectedItems);

                                    flag ? copy.remove(item.value) : copy.add(item.value);

                                    onChanged(copy, item.value);
                                  }
                                }
                              : null,
                        ),
                      ))
                  .toList(),
            ),
          ),
        ),
      ),
    );
  }
}

class Chip extends StatelessWidget {
  final String title;
  final bool isSelected;
  final VoidCallback onTap;

  const Chip({
    Key key,
    @required this.title,
    this.isSelected = false,
    this.onTap,
  }) : super(key: key);

  TextStyle get textStyle => isSelected ? TextStyleConstant().white_26 : TextStyleConstant().normal_1_26;

  Color get bgColor => isSelected ? ColorConstant.ff5770f6 : Colors.transparent;

  Border get border => isSelected
      ? null
      : Border.all(
          color: ColorConstant.e6e6,
          width: 1.px,
        );

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.translucent,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(
              horizontal: 19.w,
              vertical: 3,
            ),
            decoration: BoxDecoration(
              color: bgColor,
              borderRadius: BorderRadius.circular(
                25.w,
              ),
              border: border,
            ),
            constraints: BoxConstraints(
              minWidth: 90.w,
            ),
            child: Text(
              title,
              style: textStyle,
            ),
          )
        ],
      ),
    );
  }
}
